package week3;
import java.util.Scanner;
import java.util.Random;
public class report2 {
    public static void main(String[] args) {
        Random random = new Random();
        int targetNumber = random.nextInt(100) + 1;

        Scanner scanner = new Scanner(System.in);
        int guess;
        int count = 0;

        System.out.println("欢迎来到猜数字游戏！");
        System.out.println("我已经想好了一个1到100之间的数字，请你来猜一猜。");
        do {
            System.out.print("请输入你猜的数字：");
            while (!scanner.hasNextInt()) {
                System.out.println("输入无效，请输入一个整数！");
                scanner.next();
                System.out.print("请重新输入你猜的数字：");
            }
            guess = scanner.nextInt();
            count++;
            if (guess < targetNumber) {
                System.out.println("猜小了！再试试更大的数字。");
            } else if (guess > targetNumber) {
                System.out.println("猜大了！再试试更小的数字。");
            } else {
                System.out.println("恭喜你，猜对了！");
                System.out.println("你一共猜了" + count + "次。");
            }
        } while (guess != targetNumber);
        scanner.close();
    }
}

